package cn.liulin.llgateway.filter.factory;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * cn.liulin.llgateway.filter.factory$
 *
 * @author ll
 * @date 2022-01-24 10:42:00
 **/
@Component
public class TraceIdResponseGatewayFilterFactory extends AbstractGatewayFilterFactory {

    @Resource
    private brave.Tracer tracer;

    @Override
    public GatewayFilter apply(Object config) {
        return ((exchange, chain) -> {
            ServerHttpResponse originalResponse = exchange.getResponse();
            HttpHeaders headers = originalResponse.getHeaders();
            headers.add("scd-trace-id", tracer.currentSpan().toString());
            // 在then方法里的，相当于aop中的后置通知
            return chain.filter(exchange);
        });
    }
}
